/**
 * @company : 版权所属 四川北星惯牌科技有限公司
 * @contact : 刘经理 19326416371
 **/
// 全局请求封装
// const base_url = 'http://xq.minpion.com/api'
// var baseUrl = process.env.NODE_ENV === 'development' ? '/api' : 'http://api.youailove.com';
// var baseUrl =  '/api' 
var baseUrl = 'https://gapi.jixingge.com';
import {
	userInfoStore
} from "@/stores/user";
// 需要修改token，和根据实际修改请求头
const request = (url, method, params, loading = false) => {
	uni.getNetworkType({
		success: function(res) {
			if (res.networkType === "none") {
				// console.log("当前无网络");
				uni.showToast({
					title: '请检查网络连接',
					icon: 'none'
				})
			} else {}
		}
	});
	// 获取本地token
	const tokens = uni.getStorageSync('user_token');
	//判断进入页面的时候或者本地有没有token,有就直接值某一页，没有则请求接口
	if (loading) {
		uni.showLoading({
			title: "加载中"
		});
	}

	// 根据请求方法或URL来判断是否添加请求头
	var header = {}
	if (method == "post") {
		header['content-type'] = "application/json";
		// header['content-type'] = "application/x-www-form-urlencoded";
		header['token'] = tokens;
	} else {
		header['content-type'] = "application/json";
		header['token'] = tokens;
	}
	return new Promise((resolve, reject) => {
		uni.request({
			url: baseUrl + url,
			method: method,
			header: header,
			data: params,
			success(response) {
				const res = response;
				if (res.statusCode == 401) {
					const userStore = userInfoStore();
					uni.showToast({
						title: '登录失效，请重新登录',
						icon: 'none'
					});
					userStore.removeUserinfo();
					uni.removeStorageSync('user_token');
					setTimeout(() => {
						uni.reLaunch({
							url: '/pages/login/login'
						})
					}, 1200)
					return
				}

				resolve(res.data);
			},
			fail(err) {
				reject(err);
			},
			complete(a) {
				if (loading) {
					setTimeout(() => {
						uni.hideLoading();
					}, 600)
				}
			}
		});
	}).catch((e) => {});
};
export default request;